﻿using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using TeBigCoreApi.Common;
using TeBigScreenWpf.ViewModels;
using Wings.NativeInterop;

namespace TeBigScreenWpf.Pages
{
    /// <summary>
    /// Interaction logic for Page17.xaml
    /// </summary>
    public partial class Page17 : Page
    {
        private double DefaultScale;

        public Page17()
        {
            InitializeComponent();

            this.Loaded += Page_Loaded;

            this.DataContext = vmGlobal.vmPage.Value;
        }

        private void Value_CaseXYLoaded(object sender, RoutedEventArgs e)
        {
            RenderContent();
        }

        private void Page_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            RenderContent();
        }

        private void RestoreMapZoom()
        {
            var rect = new Rect(122.522972217755, 45.4248047848192, 3.66945357079578, 1.89817313630427);

            this.GeoMap.ZoomToGeographic(rect);

            DefaultScale = this.GeoMap.ActualWindowScale;

            //禁止移动
            //this.GeoMap.Zoomable = false;

            this.GeoMap.WindowRectChanged += GeoMap_WindowRectChanged;
        }

        private void GeoMap_WindowRectChanged(object sender, Infragistics.RectChangedEventArgs e)
        {
            RenderContent();
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            RestoreMapZoom();

            RenderContent();

            this.SizeChanged += Page_SizeChanged;

            vmGlobal.vmPage.Value.CaseHeatLoaded += Value_CaseXYLoaded;
        }

        /// <summary>
		/// Renders random heat map
		/// </summary>
		private void RenderContent()
        {
            uint w = (uint)this.GeoMap.ActualWidth, h = (uint)this.GeoMap.ActualHeight;

            var zoom = GeoMap.ActualWindowScale / DefaultScale;

            var items = vmGlobal.vmPage.Value.CaseHeat;
            if (items != null && items.Count > 0)
            {
                var hm = HeatMap.new_heatmap(w, h);
                var st = HeatMap.stamp_gen(12);

                foreach (var xy in items)
                {
                    var pos = this.GeoMap.GetWindowPoint(new Point(xy.Longitude, xy.Latitude));

                    HeatMap.add_point_with_stamp(hm, (uint)(pos.X), (uint)(pos.Y), st);
                }

                byte[] array = new byte[w * h * 4];
                {
                    int size = Marshal.SizeOf(array[0]) * array.Length;

                    IntPtr data = Marshal.AllocHGlobal(size);

                    HeatMap.render_default_to(hm, data);

                    Marshal.Copy(data, array, 0, array.Length);

                    Marshal.FreeHGlobal(data);  
                }

                HeatMap.stamp_free(st);
                HeatMap.free_heatmap(hm);

                var bitmapSource = new RgbaBitmapSource(array, (int)w);

                imgHeatMap.Source = bitmapSource;
            }
        }
    }
}